package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.payconfig.EntityFoodstuffPurchase;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.payconfig.food.GetFoodPurchaseListRequest;
import com.ocom.common.request.payconfig.food.RemoveFoodPurchaseRequest;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoFoodstuffPurchase;
import com.ocom.payconfig.interceptor.ScopeQueryWrapper;
import com.ocom.payconfig.service.FoodstuffPurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FoodstuffPurchaseServiceImpl extends ServiceImpl<IDaoFoodstuffPurchase, EntityFoodstuffPurchase> implements FoodstuffPurchaseService {

    @Autowired
    IDaoFoodstuffPurchase iDaoFoodstuffPurchase;

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public PageInfo<EntityFoodstuffPurchase> getList(Long companyId, GetFoodPurchaseListRequest request) {
        PageHelper.startPage(request.getPage(), request.getSize());
        List<EntityFoodstuffPurchase> foodstuffInfos = iDaoFoodstuffPurchase.getList(request);
        PageInfo<EntityFoodstuffPurchase> pageInfo=new PageInfo<>(foodstuffInfos);
        return pageInfo;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean removeFoodPurchase(Long companyId, RemoveFoodPurchaseRequest request) {
        ScopeQueryWrapper<EntityFoodstuffPurchase> queryWrapper = new ScopeQueryWrapper<>();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("merch_id", request.getMerchId());
        queryWrapper.eq("purchase_id", request.getPurchaseId());
        return this.remove(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean newFoodPurchase(Long companyId, EntityFoodstuffPurchase entityRestStaff) {
        return this.save(entityRestStaff);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean updateFoodPurchase(Long companyId, EntityFoodstuffPurchase entityRestStaff) {
        return this.updateById(entityRestStaff);
    }
}
